package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Override
    public void pageRoomItemByQusry(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo pageRoomItemGetById(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null) {
            return null;
        }
        //1公寓
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
        //2房间号
        String roomItemVos = String.valueOf(roomInfoMapper.selectquById(id));
        //3图片
        List<GraphVo> graphVos = graphInfoMapper.selectquByid(ItemType.ROOM, id);
        //4标签
        List<LabelInfo> labelInfos = labelInfoMapper.selectquByid(id);
        //5配套
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectquByid(id);
        //6属性
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectqquByid(id);
        //7杂费
        List<FeeValueVo> feeValueVos = feeValueMapper.selectqquByid(apartmentItemVo.getId());
        //8租期
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectquByid(id);
        //9.支付方式
        List<PaymentType> paymentTypes = paymentTypeMapper.selectquByid(id);
        //10.入住状态
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        lambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNING, LeaseStatus.WITHDRAWING);
        Long l = leaseAgreementMapper.selectCount(lambdaQueryWrapper);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //复制BeanUtils 提供的一个工具方法 要求 roomInfo 和 appRoomDetailVo 有相同或兼容的属性名和类型，否则不会复制。
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(l > 0);
        roomDetailVo.setId(id);
        roomDetailVo.setRoomNumber(roomItemVos);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFeeValueVoList(feeValueVos);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setPaymentTypeList(paymentTypes);

        return roomDetailVo;
    }
}




